<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

  <div id="root"></div>

  <script src="../lib/react.js"></script>
  <script src="../lib/react-dom.js"></script>
  <script src="../lib/babel.js"></script>

  <script type="text/babel">
    class App extends React.Component {
      constructor() {
        super()

        // 1. 通过bind绑定this
        this.btn1Click = this.btn1Click.bind(this)
      }    
      
      btn1Click() {
        console.log('btn1Click',this);
      }
      // 2. 通过ES6 class fields
      btn2Click = () => {
        console.log('btn2Click',this);
      }

      btn3Click() {
        console.log('btn3Click',this);
      }
      render() {
        return (
          <div>
            <button onClick={this.btn1Click}>按钮1</button>
            <button onClick={this.btn2Click}>按钮2</button>
            {/* 3. 直接传入箭头函数 */}
            {/* 这里是传入箭头函数，箭头函数执行方法，执行方法时进行隐式绑定，所以方法要加() */}
            <button onClick={() =>this.btn3Click()}>按钮3</button>
          </div>
        )
      }
    }


    const root = ReactDOM.createRoot(document.querySelector('#root'))
    root.render(<App />)
  </script>
</body>
</html>